/**
 * 编辑面
 */

import Edit from './Edit'
import IMG_CIRCLE_YELLOW from '../images/circle_yellow.png'
import * as turf from '@turf/turf'
import Transform from '../../transform/Transform'
import TurfTransform from '../../transform/TurfTransform'



export default class EditPolygonRound extends Edit {
  constructor(overlay) {
    super()
    this._overlay = overlay
    this._positionsRound = []
    this._positionsCoordinates = []
    this._radius = 0
    this._message = '点击鼠标结束编辑'
  }

  _mountEntity() {
    this._delegate = new Cesium.Entity()
    this._delegate.merge(this._overlay.delegate)
    this._positionsCoordinates = this._overlay._positionsCoordinates
    this._overlay.show = false
    this._delegate.polygon.hierarchy = new Cesium.CallbackProperty(time => {
      
      if(this._positionsRound.length > 1){
        return new Cesium.PolygonHierarchy(this._positionsRound)
      }
    }, false)
    this._layer.add(this._delegate)
  }

  _mountAnchor() {
    let position = this.computedMiddle()
    this._positions.push(position)
    this.createAnchor(position)
  }

  _onClick(e) {
    e = this.getMouseInfo(e.position)
    let len = this._positionsRound.length
    if (len > 1) {
      this.unbindEvent()
      this._overlay.positions = this._positionsRound
      this._overlay._positionCoordinates = this._positionsCoordinates
      this._overlay.show = true
      this._plotEvent.raiseEvent(this._overlay)
    }
  }

  _onMouseMove(e) {
    e = this.getMouseInfo(e.endPosition)
    this._tooltips.showAt(e.windowPosition , this._message)
    let position = this._clampToGround ? e.surfacePosition : e.position
    if (!position) {
      return false
    }

    this._radius = Cesium.Cartesian3.distance(
      this._positions[0],
      position
    )
    this._radius *= 1.6

    this.computedPosition(this._radius)
    let angle = this.computedPointAngle(this._positions[0] , position)
    let anglePositionRound = this.computedRotate(angle)
    this._positionsRound = new Cesium.Cartesian3.fromDegreesArray(anglePositionRound)

  }

  _onRightClick(e) {
    this.unbindEvent()
    this._overlay.positions = this._positions
    this._overlay.show = true
    this._plotEvent.raiseEvent(this._overlay)
  }


  computedMiddle() {
    var polygon = turf.polygon([ this._positionsCoordinates ]);
    var center = turf.centerOfMass(polygon)
    let corrdinates = center.geometry.coordinates
    return new Cesium.Cartesian3.fromDegrees(corrdinates[0],corrdinates[1])
  }

  /**
   * 根据半径计算多边形位置
   * @param {*} radius 
   */
   computedPosition(radius){
    let center = Transform.Cartesian3ToWGS84(this._viewer, this._positions[0])
    center = [center[0] , center[1]]
    let options = {steps: 5}
    let circle = turf.circle(center, radius / 1609 , options)
    let coordinates = circle.geometry.coordinates[0]
    let positions = TurfTransform.TurfCoordArrayToWGS84CoordArray(coordinates)
    this._positionCoordinates = coordinates
    return positions
  }
  
  /**
   * 计算两点之间角度
   * @param {*} startPoint 
   * @param {*} endPoint 
   */

  computedPointAngle(startPoint , endPoint){
    startPoint = Transform.Cartesian3ToWGS84(this._viewer, startPoint)
    startPoint = [startPoint[0] , startPoint[1]]

    endPoint = Transform.Cartesian3ToWGS84(this._viewer, endPoint)
    endPoint = [endPoint[0] , endPoint[1]]

    let point1 = turf.point(startPoint)
    var point2 = turf.point(endPoint)

    return turf.bearing(point1, point2)
  }
  
  /**
   * 计算旋转角度
   * @param {*} angle 
   */
  computedRotate(angle){
    let poly = turf.polygon( [ this._positionCoordinates ])
    let rotatedPoly = turf.transformRotate(poly, angle)
    let coordinates = rotatedPoly.geometry.coordinates[0]
    let positions = TurfTransform.TurfCoordArrayToWGS84CoordArray(coordinates)

    this._positionCoordinates = coordinates
    return positions
  }
}

